package com.nwpu.intimacy.friend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nwpu.intimacy.R;
import com.nwpu.intimacy.been.AddRequestUserInformation;
import com.nwpu.intimacy.utils.CacheUtil;

public class AddFriendRequestActivity extends Activity {

	private List<AddRequestUserInformation> friendInformations;

	private SightListAdapter sightListAdapter;

	private ListView listView;

	private SharedPreferences sp_userMessage;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addfriendlist);

		friendInformations = new ArrayList<AddRequestUserInformation>();
		sightListAdapter = new SightListAdapter();

		sp_userMessage = this.getSharedPreferences("userMessage",
				Context.MODE_PRIVATE);

		initInformation();

		listView = (ListView) findViewById(R.id.add_friendList);
		listView.setDivider(null);
		listView.setAdapter(sightListAdapter);

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				AddRequestUserInformation addRequestUserInformation = friendInformations
						.get(arg2);

				Intent intent = new Intent();

				ResponseMessageActivity.initInformation(
						addRequestUserInformation.getUsername(),
						addRequestUserInformation.getSex() + "",
						addRequestUserInformation.getArea(),
						addRequestUserInformation.getSignature(),
						stringtoBitmap(addRequestUserInformation.getPortrait()));

				intent.setClass(getApplicationContext(),
						ResponseMessageActivity.class);
				startActivity(intent);

				finish();
			}
		});

	}

	private void initInformation() {

		try {
			int i;

			AddRequestUserInformation addRequestUserInformation;

			String str = CacheUtil
					.getFromFile(sp_userMessage.getString("USER_NAME", "")
							+ "requestFriend", "Request");

			JSONArray jsonArray = new JSONArray(str);

			for (i = 0; i < jsonArray.length(); i++) {

				JSONObject jsonObject = (JSONObject) jsonArray.get(i);

				addRequestUserInformation = new AddRequestUserInformation();

				addRequestUserInformation.setArea(jsonObject.optString("area"));
				addRequestUserInformation.setUsername(jsonObject
						.optString("username"));
				addRequestUserInformation.setPortrait(jsonObject
						.optString("portrait"));
				addRequestUserInformation.setSex(jsonObject.optInt("sex"));
				addRequestUserInformation.setSignature(jsonObject
						.optString("signature"));

				friendInformations.add(addRequestUserInformation);

			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public class SightListAdapter extends BaseAdapter {

		@SuppressLint("ViewHolder")
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			LayoutInflater mInflator = AddFriendRequestActivity.this
					.getLayoutInflater();
			convertView = mInflator.inflate(R.layout.contactslist_adapter_chat,
					null);

			final AddRequestUserInformation addRequestUserInformation = friendInformations
					.get(position);

			TextView nameTextView = (TextView) convertView
					.findViewById(R.id.user_friend_name);
			nameTextView.setText(addRequestUserInformation.getUsername());

			ImageView head_user = (ImageView) convertView
					.findViewById(R.id.head_user);
			Bitmap bitmap = stringtoBitmap(addRequestUserInformation
					.getPortrait());
			Drawable drawable = new BitmapDrawable(bitmap);
			head_user.setImageDrawable(drawable);

			return convertView;
		}

		@Override
		public int getCount() {
			return friendInformations.size();
		}

		@Override
		public Object getItem(int position) {
			return friendInformations.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

	}

	/**
	 * 将字符串转换成Bitmap图片
	 * 
	 * @param string
	 * @return
	 */
	public Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

}
